package com.hwadee.springboot_lx.service;

import com.hwadee.springboot_lx.Repository.AnalysisRepository;
import com.hwadee.springboot_lx.Repository.BehaviorRepository;
import com.hwadee.springboot_lx.Repository.GradeRepository;
import com.hwadee.springboot_lx.Repository.StudentRepository;
import com.hwadee.springboot_lx.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ResourceService {
    @Autowired
    private AnalysisRepository analysisRepository;
    @Autowired
    private BehaviorRepository behaviorRepository;
    @Autowired
    private GradeRepository gradeRepository;
    @Autowired
    private StudentRepository studentRepository;

    public Student getStudentDetail(Long id){
        Student student = studentRepository.findById(id);
        return student;
    }

    public List<Grade> getGradeDetails(Long id){
        List<Grade> grades = gradeRepository.findByStudentID_Id(id);
        return grades;
    }

    public List<Analysis> getAnalysisDetails(Long id){
        List<Analysis> analysisList = analysisRepository.findByStudentID_Id(id);
        return analysisList;
    }

    public List<Behavior> getBehaviorDetails(Long id){
        List<Behavior> behaviorList = behaviorRepository.findByStudentID_Id(id);
        return behaviorList;
    }

    public R login(String account, String password) {
        //从数据库中通过用户名查询用户信息,并将查询结果和密码比对
        Student student =studentRepository.findByAccount(account);
        if(student != null){
            if(student.getStudentAccount().equals(account) && student.getPassword().equals(password)){
                return R.ok().msg("登录成功").data(student);
            }
        }
        return R.error(400).msg("用户名或密码错误");
    }

}
